package com.example.occlusiondemo.view;

import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

/**
 * 自定义的手势
 * <p>
 *     通用的
 * </p>
 * <pre>SampleCode:
 * </pre>
 *
 * @author tanyx 2022/9/29
 * @version 1.0
 **/
public class CustomGesture extends GestureDetector.SimpleOnGestureListener {
    public interface Callback{
        boolean onDoubleTap(MotionEvent e);
        void onShowPress(MotionEvent e);
        void onDown(MotionEvent e);
        void onScaleX(float scaleFactor);
        void onScaleY(float scaleFactor);
        void onTranslation(float x,float y);
    }

    public static final String TAG = "IKKYU-DEBUG";

    private boolean firstScroll = false;
    private boolean doTranslation = false;
    private MotionEvent[] firstScrollEvent;

    private float[] originDistanceXY = new float[2];
    private float[] originPoint = new float[2];

    /**
     * 双指的初始方向，决定调哪个方向
     * 0:表示X方向，对应originDistanceXY[0]
     * 1:表示Y方向，对应originDistanceXY[1]
     * -1:表示不是双指状态
     */
    private int originDirection = -1;

    private Callback callback;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        if (callback!=null){
            return callback.onDoubleTap(e);
        }
        //去执行管线的选中
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        //状态更新
//        doTranslation = true;
        if (callback!=null){
            callback.onShowPress(e);
        }
        //这个高亮显示坑洞框，表示要修改尺寸了，这种不靠谱，仅在单指时有效
    }

    @Override
    public boolean onDown(MotionEvent e) {
        //这是每次手势最先执行的，可做状态重置
        //状态重置
//        doTranslation = false;
        doTranslation = true;
        firstScroll = true;
        if (callback!=null){
            callback.onDown(e);
        }
        return true;
    }


    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        int pointerCount = e2.getPointerCount();
        if (firstScroll){
            //记录初始状态
            firstScrollEvent = new MotionEvent[]{e1,e2};

            switch (pointerCount){
                case 2:
                    doTranslation = false;//当使用了双指，则本次触摸交互的事件不再有translation动作
                    originDistanceXY[0] = Math.abs(e2.getX(1) - e2.getX(0));
                    originDistanceXY[1] = Math.abs(e2.getY(1) - e2.getY(0));

                    //这里是需要记录初始方向
                    if (originDistanceXY[1] > originDistanceXY[0]){
                        //y方向大于x方向，则更新方向标记
                        originDirection = 1;
                    }else {
                        originDirection = 0;
                    }
                    break;
                case 1:
                    doTranslation = true;
                    originPoint[0] = e2.getX();
                    originPoint[1] = e2.getY();
                    originDirection = -1;
                    break;
            }

            firstScroll = false;
        }


        //实时状态
        switch (pointerCount){
            case 2:
                if (doTranslation)break;
                float x = Math.abs(e2.getX(1) - e2.getX(0));
                float y = Math.abs(e2.getY(1) - e2.getY(0));
                switch (originDirection){
                    case 0:
                        if (callback!=null){
                            callback.onScaleX(x / originDistanceXY[0]);
                        }
                        break;
                    case 1:
                        if (callback!=null){
                            callback.onScaleY(y / originDistanceXY[1]);
                        }
                        break;
                }
                break;
            case 1:
                if (!doTranslation)break;
                if (callback!=null){
                    callback.onTranslation(e2.getX() - originPoint[0],e2.getY() - originPoint[1]);
                }
                break;
        }

        return false;
    }
}
